﻿using System;
using System.Collections;
using System.Data;
namespace GeneralLibs.Lists
{
    public class NguoiDungCuoi : DoiTuong
    {
        #region string MaKH property
        public string MaKH
        {
            get
            {
                if (_DataRow["MaKH"] is string) return (string)_DataRow["MaKH"];
                return "";
            }
            set
            {
                _DataRow["MaKH"] = value;
            }
        }
        #endregion

        #region string HoTenPhuHuynh property
        public string HoTenPhuHuynh
        {
            get
            {
                if (_DataRow["HoTenPhuHuynh"] is string) return (string)_DataRow["HoTenPhuHuynh"];
                return "";
            }
            set
            {
                _DataRow["HoTenPhuHuynh"] = value;
            }
        }
        #endregion

        #region string HoTenBe property
        public string HoTenBe
        {
            get
            {
                if (_DataRow["HoTenBe"] is string) return (string)_DataRow["HoTenBe"];
                return "";
            }
            set
            {
                _DataRow["HoTenBe"] = value;
            }
        }
        #endregion

        #region DateTime? NgayThangNamSinh property
        public DateTime? NgayThangNamSinh
        {
            get
            {
                if (_DataRow["NgayThangNamSinh"] is DateTime)
                    return (DateTime)_DataRow["NgayThangNamSinh"];
                else
                    return null;
            }
            set
            {
                _DataRow["NgayThangNamSinh"] = value;
            }
        }
        #endregion

        #region int NamSinh property
        public int NamSinh
        {
            get
            {
                if (_DataRow["NamSinh"] is int) return (int)_DataRow["NamSinh"];
                return -1;
            }
            set
            {
                _DataRow["NamSinh"] = value;
            }
        }
        #endregion

        #region string DiaChi property
        public string DiaChi
        {
            get
            {
                if (_DataRow["DiaChi"] is string) return (string)_DataRow["DiaChi"];
                return "";
            }
            set
            {
                _DataRow["DiaChi"] = value;
            }
        }
        #endregion

        #region string DienThoai property
        public string DienThoai
        {
            get
            {
                if (_DataRow["DienThoai"] is string) return (string)_DataRow["DienThoai"];
                return "";
            }
            set
            {
                _DataRow["DienThoai"] = value;
            }
        }
        #endregion

        #region string Email property
        public string Email
        {
            get
            {
                if (_DataRow["Email"] is string) return (string)_DataRow["Email"];
                return "";
            }
            set
            {
                _DataRow["Email"] = value;
            }
        }
        #endregion

        public NguoiDungCuoi(DataRow dataRow)
            : base(dataRow)
        {
            //this.TrangThai = true;
        }
    }

    public class DanhMucNguoiDungCuoi : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucNguoiDungCuoi _Instance;
        public static DanhMucNguoiDungCuoi Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucNguoiDungCuoi();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                }
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "NguoiDungCuoi";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new NguoiDungCuoi((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public NguoiDungCuoi this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new NguoiDungCuoi(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public NguoiDungCuoi this[int indexView, bool a]
        {
            get
            {
                if (0 <= indexView && indexView <= this.Count)
                {
                    return new NguoiDungCuoi(DataView.ToTable().Rows[indexView]);
                }
                else
                {
                    return null;
                }
            }
        }

        public NguoiDungCuoi NewDoiTuong()
        {
            return new NguoiDungCuoi(DataTable.NewRow());
        }

        public NguoiDungCuoi GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new NguoiDungCuoi(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public NguoiDungCuoi GetByID(Guid objID)
        {
            return GetByProperty("ID", objID);
        }

        public static DanhMucNguoiDungCuoi GetAll()
        {
            return Instance;
        }

        public static DanhMucNguoiDungCuoi Filter(string filterExpression)
        {
            try
            {
                DanhMucNguoiDungCuoi obj = new DanhMucNguoiDungCuoi();
                obj.DataView = new DataView(DanhMucNguoiDungCuoi.Instance.DataTable, filterExpression, "PhieuID", DataViewRowState.CurrentRows);
                return obj;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static NguoiDungCuoi GetByPhieuIDHoTenPhuHuynh(string hoTenPhuHuynh)
        {
            try
            {
                DanhMucNguoiDungCuoi dm = Filter(string.Format("HoTenPhuHuynh = '{0}'", hoTenPhuHuynh));
                if (dm.Count >= 1) return dm[0, true];
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static NguoiDungCuoi GetByPhieuIDHoTenBe(string hoTenBe)
        {
            try
            {
                DanhMucNguoiDungCuoi dm = Filter(string.Format("HoTenBe = '{0}'", hoTenBe));
                if (dm.Count >= 1) return dm[0, true];
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

    }
}
